//
//  ZKBaseButton.m
//  VideoScreenApp
//
//  Created by 凯朱 on 2022/5/30.
//

#import "ZKBaseButton.h"

@interface ZKBaseButton()
@property (nonatomic, strong) UIColor *startColor;
@property (nonatomic, strong) UIColor *endColor;
@end

@implementation ZKBaseButton

- (void)layoutSubviews {
    [super layoutSubviews];
   
 
}

-(void)setBtnStyle:(NSInteger)btnStyle{
    _btnStyle = btnStyle;
    if (_btnStyle == 1) {//渐变填充色
        self.layer.cornerRadius = self.height/2;
        self.layer.masksToBounds = YES;
        self.titleLabel.font = kFontBold(16);
        [self setTitleColor:KColor_1 forState:UIControlStateNormal];
        _startColor = KColor_O;
        _endColor = KColor_Y;
        [self updateLayout];
    }else if (_btnStyle == 2){//非填充色
        self.layer.cornerRadius = self.height/2;
        self.layer.masksToBounds = YES;
        self.layer.borderColor = KColor_O.CGColor;
        self.layer.borderWidth = 1;
        self.backgroundColor = KClearColor;
        self.titleLabel.font = kFontBold(16);
        [self setTitleColor:KColor_O forState:UIControlStateNormal];
    }
}

-(void)setLineTextWith:(NSString *)title{
    NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc] initWithString:title];
    NSRange titleRange = {0,[title length]};
    [titleStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
    [self setAttributedTitle:titleStr forState:UIControlStateNormal];
}

-(void)updateLayout{
    // 创建渐变图层
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawLinearGradient(context,
                                CGGradientCreateWithColors(NULL,
                                                           (__bridge CFArrayRef)@[(id)self.startColor.CGColor, (id)self.endColor.CGColor],
                                                           (CGFloat[]){0, 1}),
                                CGPointMake(0, 0),
                                CGPointMake(self.bounds.size.width, 0),
                                kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self setBackgroundImage:gradientImage forState:UIControlStateNormal];
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    if (!CGRectIsEmpty(self.titleRect) && !CGRectEqualToRect(self.titleRect, CGRectZero)){
        return self.titleRect;
    }
    return [super titleRectForContentRect:contentRect];
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    if (!CGRectIsEmpty(self.imageRect)&&!CGRectEqualToRect(self.imageRect, CGRectZero)) {
        return self.imageRect;
    }
    return [super imageRectForContentRect:contentRect];
}



@end
